package com.work.lxykeeper.module.pagemine;

import android.text.TextUtils;
import android.view.View;

import com.work.lxykeeper.R;
import com.work.lxykeeper.base.BaseActivity;
import com.work.lxykeeper.bean.CommonBean;
import com.work.lxykeeper.bean.PhoneCheck;
import com.work.lxykeeper.databinding.ActivityPagemineRegisterBinding;
import com.work.lxykeeper.httpretrofit.MyCallback;
import com.work.lxykeeper.httpretrofit.PostBody;
import com.work.lxykeeper.httpretrofit.RetrofitManager;
import com.work.lxykeeper.utils.UIUtils;
import com.work.lxykeeper.utils.VerificationUtil;
import com.work.lxykeeper.utils.ViewUtils;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

/**
 * author: Aly
 * date :2018/12/7 09:30
 * version:1.0
 * description:注册
 */
public class RegisterActivity extends BaseActivity {

    private ActivityPagemineRegisterBinding binding;
    private int type;


    @Override
    protected View getLayoutView() {
        binding= ActivityPagemineRegisterBinding.inflate(getLayoutInflater());
        return null;
    }

    @Override
    protected void initView() {
        type = getIntent().getIntExtra("type", 0);
        if (type == 0) {
            mTitleBar.titleMiddle.setText("注册");
            binding.tvRegister.setText("注册");
        } else {
            mTitleBar.titleMiddle.setText("修改密码");
            binding.tvRegister.setText("修改密码");
        }
        ViewUtils.setOnClickListeners(this, binding.cbtTime, binding.tvAgreement, binding.tvRegister, binding.tvGologin);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.cbt_time:
                //获取短信验证码
                if (VerificationUtil.isPhone(binding.etPhone.getText().toString())) {
                    getHttpCode(binding.etPhone.getText().toString());
                    binding.cbtTime.start();
                }
                break;
            case R.id.tv_agreement:
                //用户注册协议

                break;
            case R.id.tv_register:
                if (TextUtils.isEmpty(binding.etPhone.getText().toString()) || TextUtils.isEmpty(binding.etPassword.getText().toString())) {
                    UIUtils.showToast("所填内容不能为空");
                    return;
                }
                if (type == 0) {
                    //注册
                    showLoaddialog();
                    //注册
                    getHttpRegister(binding.etPhone.getText().toString(),binding.etPassword.getText().toString());
//                    getHttpchecksmscode();
                } else {
                    //修改密码
                    getHttpRegister(binding.etPhone.getText().toString(), binding.etPassword.getText().toString());
//                    getHttpchecksmscode();
                }
                break;
            case R.id.tv_gologin:
                //登录
                finish();
                break;

        }
    }

    //校验验证码
    private void getHttpchecksmscode() {
        final String phone = binding.etPhone.getText().toString();
        final String password = binding.etPassword.getText().toString();
        Map<String, String> map = new HashMap<>();
        map.put("user_phone", phone);
        map.put("smscode", binding.etPhonecode.getText().toString());
        Call<PhoneCheck> call = RetrofitManager.getInstance().checksmscode(PostBody.toBody(map));
        call.enqueue(new MyCallback<PhoneCheck>() {
            @Override
            public void onSuccess(PhoneCheck response) {
                if (response.getErrcode() == 0) {
                    //验证码校验成功
                    if (type == 0) {
                        //注册
                        getHttpRegister(phone, password);
                    } else {
                        //修改密码
                        getHttpUpdatePassword(phone, password);

                    }
                } else {
                    UIUtils.showToast("验证码验证失败");
                    hideLoaddialog();
                }

            }


            @Override
            public void onFailure(int errorCode, String errMsg) {
                UIUtils.showToast("errMsg");
                hideLoaddialog();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    //更新密码
    private void getHttpUpdatePassword(String phone, String password) {
        showLoaddialog();
        Map<String, String> map = new HashMap<>();
        map.put("user_phone", phone);
        map.put("user_secret", password);
        Call<CommonBean> call = RetrofitManager.getInstance().updatepassword(PostBody.toBody(map));
        call.enqueue(new MyCallback<CommonBean>() {
            @Override
            public void onSuccess(CommonBean response) {
                if (response.getMeta().getCode() == 0) {
                    UIUtils.showToast("密码修改成功");
                    finish();
                } else {
                    UIUtils.showToast("密码修改失败");
                }

            }

            @Override
            public void onFailure(int errorCode, String errMsg) {

            }

            @Override
            public void onComplete() {
                hideLoaddialog();
            }
        });

    }

    //注册
    private void getHttpRegister(String phone, String password) {
        Map<String, String> map = new HashMap<>();
        map.put("userName", phone);
        map.put("password", password);
        Call<CommonBean> call = RetrofitManager.getInstance().register(PostBody.toBody(map));
        call.enqueue(new MyCallback<CommonBean>() {
            @Override
            public void onSuccess(CommonBean response) {
                if (response.getMeta().getCode() == 0) {
                    UIUtils.showToast("注册成功,请登录");
                    finish();
                } else {
                    UIUtils.showToast(response.getMeta().getMessage());
                }

            }

            @Override
            public void onFailure(int errorCode, String errMsg) {
            }

            @Override
            public void onComplete() {
                hideLoaddialog();
            }
        });

    }

    //获取验证码
    private void getHttpCode(String phone) {
        Map<String, String> map = new HashMap<>();
        map.put("user_phone", phone);
        Call<Object> call = RetrofitManager.getInstance().getsmscode(PostBody.toBody(map));
        call.enqueue(new MyCallback<Object>() {
            @Override
            public void onSuccess(Object response) {
                UIUtils.showToast("验证码发送成功");
            }

            @Override
            public void onFailure(int errorCode, String errMsg) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
